package com.shortvedio.ucenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shortvedio.ucenter.feignclient.CheckCodeClient;
import com.shortvedio.ucenter.mapper.SUsersLoginMapper;
import com.shortvedio.ucenter.model.Result;
import com.shortvedio.ucenter.model.dto.AuthDto;
import com.shortvedio.ucenter.model.dto.TokenDto;
import com.shortvedio.ucenter.model.po.SUsersLogin;
import com.shortvedio.ucenter.properties.JwtProperties;
import com.shortvedio.ucenter.service.AuthService;
import com.shortvedio.ucenter.utils.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Mr.M
 * @version 1.0
 * @description 账号名密码方式
 * @date 2023/2/24 11:56
 */
@Service
@Slf4j
public class AuthServiceImpl implements AuthService {


    @Autowired
    SUsersLoginMapper sUsersLoginMapper;

    @Autowired
    CheckCodeClient checkCodeClient;  //远程调用

    @Autowired
    private JwtProperties jwtProperties;

    @Override
    public Result passwordLogin(AuthDto authDto) {
        //账号
        String username = authDto.getUsername();


        //输入的验证码
        String checkcode = authDto.getCheckcode();
        //验证码对应的key
        String checkcodekey = authDto.getCheckcodekey();

        if (StringUtils.isEmpty(checkcode) || StringUtils.isEmpty(checkcodekey)) {
            return Result.error("请输入的验证码");
        }

        //远程调用验证码服务接口去校验验证码
        Boolean verify = checkCodeClient.verify(checkcodekey, checkcode);
        if (verify == null || !verify) {
            return Result.error("验证码输入错误");
        }
        //账号是否存在
        //根据username账号查询数据库
        LambdaQueryWrapper<SUsersLogin> sUsersLoginLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sUsersLoginLambdaQueryWrapper.eq(SUsersLogin::getUserName, username);
        log.info("username:{}",username);
        SUsersLogin sUsersLogin = sUsersLoginMapper.selectOne(sUsersLoginLambdaQueryWrapper);
        //查询到用户不存在，要返回null即可
        if (sUsersLogin == null) {
            return Result.error("账号不存在");
        }
        //验证密码是否正确
        //如果查到了用户拿到正确的密码
        String passwordDb = sUsersLogin.getPassword();
        //加密
        String password = DigestUtils.md5DigestAsHex(authDto.getPassword().getBytes());
        log.info("password:{}",password);
        if (!passwordDb.equals(password)){
            return Result.error("用户密码错误");
        }
        //获取token返回
        Map<String,Object> cliams=new HashMap<>();
        cliams.put("userid",sUsersLogin.getId());
        String token= JwtUtil.createJWT(jwtProperties.getUserSecretKey(),jwtProperties.getUserTtl(),cliams);
        TokenDto tokenDto = new TokenDto();
        tokenDto.setToken(token);
        tokenDto.setUserName(sUsersLogin.getUserName());
        return Result.success(tokenDto);
    }
}